package cn.learn.controller.cargo;

import cn.learn.base.BaseController;
import cn.learn.domain.cargo.Invoice;
import cn.learn.domain.cargo.InvoiceExample;
import cn.learn.service.cargo.InvoiceService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/cargo/invoice", name = "发票信息管理")
public class InvoiceControlller extends BaseController {

    @Reference
    private InvoiceService invoiceService;

    /**
     * 查询所有发票信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", name = "查询所有发票信息")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {
        InvoiceExample invoiceExample = new InvoiceExample();
        InvoiceExample.Criteria criteria = invoiceExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        PageInfo invoiceList = invoiceService.findAll(invoiceExample, pageNum, pageSize);
        request.setAttribute("pageList", invoiceList);
        return "/cargo/invoice/invoice-list";
    }
    @RequestMapping(value = "/toInvoice",name = "到生成发票页面")
    public String toInvoice(String id) {
        request.setAttribute("id", id);
        return "/cargo/invoice/invoice-add";
    }

    @RequestMapping(value = "/edit",name = "添加发票")
    public String edit(Invoice invoice) {
        invoice.setCompanyId(companyId);
        invoice.setCompanyName(companyName);
        invoiceService.save(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/toView",name = "查看发票")
    public String toView(String id) {
        Invoice invoice = invoiceService.findById(id);
        request.setAttribute("invoice", invoice);
        return "/cargo/invoice/invoice-view";
    }

    @RequestMapping(value = "/toUpdate",name = "到修改发票页面")
    public String toUpdate(String id) {
        Invoice invoice = invoiceService.findById(id);
        request.setAttribute("invoice", invoice);
        return "/cargo/invoice/invoice-update";
    }

    @RequestMapping(value = "/update", name = "修改发票")
    public String update(Invoice invoice) {
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/submit", name = "提交发票")
    public String submit(String id) {
        Invoice invoice = invoiceService.findById(id);
        invoice.setState(1);
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/cancel", name = "取消提交发票")
    public String cancel(String id) {
        Invoice invoice = invoiceService.findById(id);
        invoice.setState(0);
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除发票")
    public String delete(String id) {
        invoiceService.delete(id);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/toFinance", name = "获取发票状态并返回")
    @ResponseBody
    public String toFinance(String id) {
        Invoice invoice = invoiceService.findById(id);
        return invoice.getState().toString();
    }

}
